<?php
/**
 * @version			$Id$
 * @author			Richard Asp
 * @package			Joomla
 * @subpackage		gnudoor
 * @copyright		(C) 2010 gnudoor LLC
 * @license      GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * See COPYRIGHT.php for copyright notices and details.
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );
jimport( 'joomla.application.component.model' );

class GnudoorModelAppt extends JModel
{
		public $events;
		public $timezone;
		public $kTimeslots;
		public $summary;
		public $dtend;
		public $dtstart;
		public $recurring;
		public $description;
		
		function __construct() {
            parent::__construct();
        }

		function &getSlots() {
		$query =  ' SELECT * '
				. ' FROM #__gnudoor_slots '
				. ' ORDER by id '
				;
		$this->_db->setQuery( $query );
		return $this->_db->loadAssocList();
		}
				
		function getArticleID($articlealias = null) {
			$query = ' SELECT id '
				. ' FROM #__content '
				. ' WHERE alias="' . $articlealias . '"'
				;
			$this->_db->setQuery($query);
		    return $this->_db->loadObject();
			
		}		
						
    /**
     * getStartDate() will return the first
     * date of a week or a month.  The date
     * will be the Sunday of the week or month 
     * where the date is located.
     * 
     * @param String $month_or_week acceptable values
     * 			are wday or mday
     * @param Date $dateparam acceptable values are
     * 			YYYY-mm-dd
     */
    function getStartDate( $month_or_week = 'wday',$dateparam = null ) {
		
    	if (function_exists('date_create')) {
	    	if (isset($dateparam)) {
				$date = date_create($dateparam);
			} else {
				$date = date_create();
			}    	
			
	    	$arr = date_parse(date_format($date,DATE_RFC822));
	    	$timestamp = mktime($arr['hour'],$arr['minute'],$arr['second'],$arr['month'],$arr['day'],$arr['year']);
			$arr = getdate($timestamp);
			$firstday = $date;
			$firstday->modify('-'.$arr[$month_or_week].' day');
    	} else {

    		if (isset($dateparam)) {
				$p = explode("-", $dateparam);
    			$timestamp = mktime(0,0,0,$p[1],$p[2],$p[0]);
    			$date = getdate($timestamp);
			} else {
				$date = getdate();
			}    	

			$sec = $date['seconds'];
			$min = $date['minutes'];
			$hr = $date['hours'];
			$date['mday'] -= $date[$month_or_week];
    		$timestamp = mktime($hr,$min,$sec,$date['mon'],$date['mday'],$date['year']);
    		
			$firstday = $timestamp;
    	}
		return($firstday);	
	}
}
?>